// @Author yy 2024/8/20
package shengWangUtil

import (
	"encoding/base64"
	"fmt"
	rtctokenbuilder "github.com/AgoraIO/Tools/DynamicKey/AgoraDynamicKey/go/src/rtctokenbuilder2"
	"peilian-api/app/global/variable"
	"strconv"
)

func GetShenWangToken(channelName, uid string) (token string, err error) {
	// 获取环境变量 AGORA_APP_ID 的值。请确保你将该变量设为你在声网控制台获取的 App ID
	appId := variable.Config.ShengWang.Appid
	// 获取环境变量 AGORA_APP_CERTIFICATE 的值。请确保你将该变量设为你在声网控制台获取的 App 证书
	appCertificate := variable.Config.ShengWang.AppCertificate
	// 将 channelName 替换为需要加入的频道名
	//channelName := "channelName"
	//// 填入你实际的用户 ID
	//uid := uint32(2882341273)
	// Token 的有效时间，单位秒
	tokenExpirationInSeconds := uint32(3600)
	// 所有的权限的有效时间，单位秒
	privilegeExpirationInSeconds := uint32(3600)

	fmt.Println("App Id:", appId)
	fmt.Println("App Certificate:", appCertificate)
	if appId == "" || appCertificate == "" {
		fmt.Println("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE")
		return
	}
	// String 转int
	atoi, _ := strconv.Atoi(uid)
	// 生成 Token
	result, err := rtctokenbuilder.BuildTokenWithUid(appId,
		appCertificate,
		channelName,
		uint32(atoi),
		rtctokenbuilder.RoleSubscriber,
		tokenExpirationInSeconds,
		privilegeExpirationInSeconds)
	if err != nil {
		fmt.Println(err)
		return "", err
	}
	return result, nil
}

/**
*@author: yy
*@description: 生成一个客户端token
*@date :2024/8/20 下午2:16
**/
func GetCustomerToken(customerKey, customerSecret string) (customerToken string) {
	// 拼接客户 ID 和客户密钥并使用 base64 编码
	plainCredentials := customerKey + ":" + customerSecret
	bytes := []byte(plainCredentials)
	encodeToString := base64.StdEncoding.EncodeToString(bytes)
	token := "Basic " + encodeToString
	return token
}
